package com.xiaoshuidi.cloud.module.contract.controller.app.contract;

import com.xiaoshuidi.cloud.framework.common.exception.ServiceException;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageParam;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.datapermission.core.annotation.DataPermission;
import com.xiaoshuidi.cloud.module.contract.controller.app.contract.vo.ApplyReqVO;
import com.xiaoshuidi.cloud.module.contract.controller.app.contract.vo.ApplyResp;
import com.xiaoshuidi.cloud.module.contract.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.contract.service.contract.ContractApplyService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.error;

/**
 * 租客 - 合同申请
 */
@Tag(name = "APP - 合同申请")
@RestController
@RequestMapping(ApiConstants.NAME + "/contract-apply")
@Validated
//关闭权限
@DataPermission(enable = false)
public class AppContractApplyController {

    @Resource
    private ContractApplyService contractApplyService;


    /**
     * 续租、退租、延期 - 申请
     *
     * @param
     * @return
     */
    @PostMapping("/apply")
    public CommonResult<ApplyResp> apply(@RequestBody ApplyReqVO req) {
        try {
            req.setId(null);
            ApplyResp resp = contractApplyService.apply(req);
            return CommonResult.success(resp);
        } catch (ServiceException e) {
            return error(e.getCode(),e.getMessage());
        }
    }

    /**
     * 获取申请详情
     *
     * @param id
     * @return
     */
    @PostMapping("/get")
    public CommonResult<ApplyResp> get(@RequestParam("id") Long id) {
        try {
            return CommonResult.success(contractApplyService.get(id));
        }  catch (ServiceException e) {
            return error(e.getCode(),e.getMessage());
        }
    }

    /**
     * 分页列表
     *
     * @param req
     * @return
     */
    @GetMapping("/page")
    public CommonResult<PageResult<ApplyResp>> page(PageParam req) {
        try {
            return CommonResult.success(contractApplyService.appPage(req));
        }  catch (ServiceException e) {
            return error(e.getCode(),e.getMessage());
        }
    }


    /**
     * 修改申请
     *
     * @param reqVO
     * @return
     */
    @PostMapping("update")
    public CommonResult<ApplyResp> update(@RequestBody ApplyReqVO reqVO) {
        try {
            return CommonResult.success(contractApplyService.appUpdate(reqVO));
        }  catch (ServiceException e) {
            return error(e.getCode(),e.getMessage());
        }
    }

    /**
     * 撤销申请
     *
     * @param id
     * @return
     */
    @PostMapping("/revoke")
    public CommonResult<Boolean> revoke(@RequestParam("id") Long id) {
        try {
            return CommonResult.success(contractApplyService.appRevoke(id));
        }  catch (ServiceException e) {
            return error(e.getCode(),e.getMessage());
        }
    }
}
